package com.vhuomai.product.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import com.vhuomai.product.dto.ProductBasic;
import com.vhuomai.product.dto.ProductSku;
import com.vhuomai.product.dto.SpuItemEdit;
import com.vhuomai.product.entity.BaseProduct;
import com.vhuomai.product.entity.BaseSku;
import com.vhuomai.product.service.ProductService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author harris
 */
@Api(tags = "产品")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class ProductManageController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "产品列表")
    @PostMapping(value = "/product/manage/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Page<BaseProduct>> products(@RequestBody PageRequest request) {
        return ServiceResult.success(productService.findProducts(request));
    }

    @ApiOperation(value = "产品基本信息")
    @GetMapping(value = "/product/base", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<ProductBasic> baseProduct(@RequestParam("id") Integer productId) {
        return ServiceResult.success(productService.findBaseById(productId));
    }

    @ApiOperation(value = "添加产品")
    @PostMapping(value = "/product/base", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Integer> createProduct(@RequestBody ProductBasic baseProduct) {
        return ServiceResult.success(productService.addProduct(baseProduct).getId());
    }

    @ApiOperation(value = "更新基本属性")
    @PutMapping(value = "/product/base", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateProduct(@RequestBody ProductBasic baseProduct) {
        productService.updateProduct(baseProduct);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除商品")
    @DeleteMapping(value = "/product/base", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> deleteProduct(@RequestParam("id") Integer productId) {
        productService.removeProduct(productId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "产品SPU信息")
    @GetMapping(value = "/product/spu/detail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<SpuItemEdit>> productSpu(@RequestParam("id") Integer productId) {
        return ServiceResult.success(productService.findSpuValues(productId));
    }

    @ApiOperation(value = "更新产品SPU信息")
    @PutMapping(value = "/product/spu/detail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateProductSpu(@RequestParam("id") Integer productId,
                                                   @RequestBody Map<Integer, String> values) {
        productService.updateSpuValues(productId, values);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "产品SKU信息")
    @GetMapping(value = "/product/sku/detail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<ProductSku> productSku(@RequestParam("id") Integer productId) {
        return ServiceResult.success(productService.findSku(productId));
    }

    @ApiOperation(value = "更新产品SKU信息")
    @PutMapping(value = "/product/sku/detail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateProductSku(@RequestBody BaseSku sku) {
        productService.updateSku(sku);
        return ServiceResult.SUCCESS;
    }
}
